package com.ycsj.goldmedalnewconcept;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.ycsj.goldmedalnewconcept.activity.JpjMainActivity;
import com.ycsj.goldmedalnewconcept.bean.UpdateInfo;
import com.ycsj.goldmedalnewconcept.service.DownloadService;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.StringReader;

//应用选择页
public class ButtonActivity extends Activity {
	private String account;
	private String role;
	private HttpUtils httpUtils;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_button);
		
		try {
			updateVersion();
		} catch (Exception e) {
			Log.e("error", e.toString());
		}
		init();
	}

	//检查版本
	private void updateVersion() {
		// TODO Auto-generated method stub
		httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET,
				"http://139.129.57.83:8080/Appandroid/updateinfo.xml", callBack);
	}


	private RequestCallBack<String> callBack = new RequestCallBack<String>() {
		private String xmlString;

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			try {
				
			
			if(responseInfo!=null){
				xmlString = responseInfo.result;
			}
			
//			Log.e("test", xmlString);
			final UpdateInfo updateInfo = parseXML2JavaBean(xmlString);
			if(updateInfo!=null && updateInfo.getVersionName()!=null){
				
			//判断版本号
			if (!getVersionName().equals(updateInfo.getVersionName())) {
				
				final NormalDialog dialog = new NormalDialog(
						ButtonActivity.this);
				dialog.content(updateInfo.getDescription())
						.btnText("暂不", "立即更新")//
						.style(NormalDialog.STYLE_ONE)//
						.titleTextSize(23)//
						.show();
				dialog.setCanceledOnTouchOutside(false);
				dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {

					@Override
					public void onBtnLeftClick() {
						dialog.dismiss();
					}
				});
				dialog.setOnBtnRightClickL(new OnBtnRightClickL() {

					@Override
					public void onBtnRightClick() {
						dialog.dismiss();
						
						Intent intent = new Intent(ButtonActivity.this,DownloadService.class);
//						intent.putExtra("url", "http://139.129.57.83:8080/Appandroid/jinpaijun.apk");
						//负载均衡下载地址
						intent.putExtra("url", "http://m.moregolden.com:8080/Appandroid/jinpaijun.apk");
						startService(intent);
					}
				});
			}
		}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		@Override
		public void onFailure(HttpException error, String msg) {
			// TODO Auto-generated method stub
		}
	};
	
	/**
	 * 解析XML版本信息
	 */
	protected UpdateInfo parseXML2JavaBean(String xmlString) {
		UpdateInfo updateInfo = null;
		try {
			// 创建解析器工厂对象
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			// 使用工石对象产生解析器对象
			XmlPullParser parser = factory.newPullParser();
			// 设置解析的源。要解析的内容
			parser.setInput(new StringReader(xmlString));
			// 获取事件类型
			int type = parser.getEventType();
			
			while (type != XmlPullParser.END_DOCUMENT) {
				switch (type) {
				case XmlPullParser.START_TAG:
					if ("updateinfo".equals(parser.getName())) {
						updateInfo = new UpdateInfo();
//						Log.e("onSuccess", "123");
					} else if ("versionName".equals(parser.getName())) {
						updateInfo.setVersionName(parser.nextText());
					} else if ("url".equals(parser.getName())) {
						updateInfo.setUrl(parser.nextText());
					} else if ("description".equals(parser.getName())) {
						updateInfo.setDescription(parser.nextText());
					}
					
					break;
				default:
					break;
				}
				// 推进
				type = parser.next();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return updateInfo;
	}
	
	/**
	 * 显示应用的版本号
	 */
	private String getVersionName() {
		// PackageManager
		PackageManager packageManager = getPackageManager();
		// PackageInfo
		String versionName = null;
		try {
			PackageInfo packageInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			versionName = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionName;
	}
	
	
	private void init() {
		
		TextView bt_jpj = (TextView) findViewById(R.id.bt_jpj);
		TextView bt_gld = (TextView) findViewById(R.id.bt_gld);
		TextView bt_tk = (TextView) findViewById(R.id.bt_tk);
		
		account = SPUtil.getString(this, "account", "");
		role = SPUtil.getString(this, "role", "");
		if("0".equals(role)){
			bt_gld.setVisibility(bt_gld.GONE);
		}else if("4".equals(role)){
			bt_gld.setVisibility(bt_gld.GONE);
			bt_tk.setVisibility(bt_tk.GONE);
		}else if("1".equals(role)){
			bt_jpj.setVisibility(bt_jpj.GONE);
			bt_tk.setVisibility(bt_tk.GONE);
		}else if("3".equals(role)){
			bt_jpj.setVisibility(bt_jpj.GONE);
			bt_tk.setVisibility(bt_tk.GONE);
		}
		
//		//测试
//		else if("2".equals(role)){
//			bt_jpj.setVisibility(bt_jpj.GONE);
//			bt_tk.setVisibility(bt_tk.GONE);
//		}
		
		bt_jpj.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(ButtonActivity.this, JpjMainActivity.class);
				startActivity(intent);
			}
		});
		
		bt_gld.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(ButtonActivity.this, MainActivity.class);
				startActivity(intent);
			}
		});
		
		bt_tk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!"2".equals(SPUtil.getString(ButtonActivity.this, "role", ""))){
//					Toast.makeText(ButtonActivity.this, SPUtil.getString(ButtonActivity.this, "role", ""), 0).show();
					Intent intent=new Intent(ButtonActivity.this, studentpracticeActivity.class);
					startActivity(intent);
				}else{
//					Toast.makeText(ButtonActivity.this, SPUtil.getString(ButtonActivity.this, "role", ""), 0).show();
					
					Intent intent=new Intent(ButtonActivity.this, TkActivity.class);
					startActivity(intent);
				}
			}
		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	
	
	
}
